<?php
namespace Home\Controller;

class ContractController extends \Home\Controller\BaseController
{
	/**
	 * 查询注册协议
	 * @author 刘新
	 */
	public function register()
	{
		$this->display('register_contract');
	}
	
	/**
	 * 查看合同详情
	 */
	public function contractDetail()
	{
		$param = I('params');
		$id = I('data.contract_id', '', '/^\d+$/', $param);
		$investid = $param['id'];
		if(!$id) {
			$this->assign('message', '请输入合同id');
			$this->display('Error/error');
		} else {
			// 查询合同信息
			$where['id'] = $id;
			$where['status'] = array('gt', -1);
			$res = $this->returnOne('UserContract', $where);
			
			//查询成功
			if($res) {
				$res['mobile'] = $this->user['mobile'];
				$res['username'] = $this->user['mobile'];
				$userDetail = $this->returnOne('UserDetail', array('user_id' => $this->user['userId']));
				$res['realname'] = $userDetail['realname'];
				$res['social_no'] = $userDetail['social_no'];
				if($investid){
					$investinfo = M("Invest")->where(array("id"=>$investid))->find();
					$res['amount'] = $investinfo['amount'];
				}

				$this->assign('user', $res);
				$this->display($res['code']);
			
			// 查询失败
			} else {
				$this->assign('message', '合同信息查询失败');
				$this->display('Error/error');
			}
		}
	}
	/**
	 * 根据类别获取合同
	 */
	public function getcontractbycode(){
		$param = I('params');
		$code = $param['code'];
		$investid = $param['id'];

		$contract = M("Contract")->where(array("code"=>$code))->find();
		if(empty($contract)){
			$this->ajaxReturn(array('error' => 1, 'info' =>"数据为空"));
		}else{
			$where['id'] = $contract['id'];
			$where['status'] = array('gt', -1);
			$res = $this->returnOne('UserContract', $where);
			$userToken = $param['userToken'];
			$ut = array();
			if($userToken){
				$ut = $this->returnOne('UserToken', array('user_token' => $userToken),'expire_time desc','user_id,user_token,expire_time,mobile');
			}
			//查询成功
			$res['mobile'] = $ut['mobile'];
			$res['username'] = $ut['mobile'];
			$userDetail = $this->returnOne('UserDetail', array('user_id' => $ut['userId']));
			$res['realname'] = $userDetail['realname'];
			$res['social_no'] = $userDetail['social_no'];
			if($investid){
				$investinfo = M("Invest")->where(array("id"=>$investid))->find();
				$res['amount'] = $investinfo['amount'];
			}
			$this->assign('user', $res);
			$this->display($code);
		}
	}
}